﻿using PLCComHelperProj;
using System;
using System.Net.Sockets;
using System.Threading;

internal class PLCLink
{
    private Device device = new Device();
    private int int_2 = 100;
    private string IPSetAdd = FuckProtect.DataFrom(0x6ce);
    private int PLCStatue;
    private Socket socket_0 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    private bool StopFlag;
    public int TCPPort = 0x66;
    private Thread thread;

    private bool BackTrue(e_PLC_MMType e_PLC_MMType_0, e_PLC_DATA_TYPE e_PLC_DATA_TYPE_0)
    {
        return true;
    }

    public int commStatus()
    {
        return this.PLCStatue;
    }

    public Device GetDevice()
    {
        return this.device;
    }

    private void GetStatue(object object_0)
    {
        bool flag = false;
        while (!this.StopFlag)
        {
            if (!flag)
            {
                if (!this.device.ConnectToPLC(this.socket_0))
                {
                    flag = false;
                    this.PLCStatue = 5;
                    continue;
                }
                flag = true;
                this.PLCStatue = 3;
            }
            if (this.device.ProcessPLCData(this.socket_0, this.StopFlag))
            {
                this.PLCStatue = 4;
            }
            else
            {
                this.PLCStatue = 6;
                while (!this.StopFlag)
                {
                    if (this.PLCconnect())
                    {
                        flag = false;
                        break;
                    }
                    Thread.Sleep(0x3e8);
                }
            }
            Thread.Sleep(this.int_2);
        }
        this.PLCStatue = 0;
    }

    public object getValue(string str)
    {
        str = str.Trim().ToUpper();
        char[] separator = new char[] { '.' };
        string[] strArray = str.Split(separator);
        string str2 = strArray[0];
        string str3 = strArray[1];
        TagGroup group = this.device.TagGroups[str2];
        Tag tag = group.Tags[str3];
        switch (tag.CheckDataType())
        {
            case e_PLC_DATA_TYPE.TYPE_INT:
                return this.method_13(str2, Convert.ToInt32(tag.Address));

            case e_PLC_DATA_TYPE.TYPE_FLOAT:
                return this.method_14(str2, Convert.ToInt32(tag.Address));

            case e_PLC_DATA_TYPE.TYPE_BOOL:
            {
                char[] chArray2 = new char[] { '.' };
                string[] strArray2 = tag.Address.Split(chArray2);
                int num = Convert.ToInt32(strArray2[0]);
                int num2 = Convert.ToInt32(strArray2[1]);
                return this.method_16(str2, num, num2);
            }
            case e_PLC_DATA_TYPE.TYPE_SHORT:
                return this.method_15(str2, Convert.ToInt32(tag.Address));

            case e_PLC_DATA_TYPE.TYPE_BYTE:
                return this.method_12(str2, Convert.ToInt32(tag.Address));
        }
        return -9999f;
    }

    public string method_0()
    {
        return this.IPSetAdd;
    }

    private int method_12(string string_1, int int_3)
    {
        return this.device.TagGroups[string_1].GetByteData(int_3);
    }

    private int method_13(string string_1, int int_3)
    {
        return this.device.TagGroups[string_1].GetIntData(int_3);
    }

    private float method_14(string string_1, int int_3)
    {
        return this.device.TagGroups[string_1].GetFloatData(int_3);
    }

    private short method_15(string string_1, int int_3)
    {
        return this.device.TagGroups[string_1].GetShortValue(int_3);
    }

    private int method_16(string string_1, int int_3, int int_4)
    {
        return this.device.TagGroups[string_1].GetBitData(int_3, int_4);
    }

    public void method_3(Device device_1)
    {
        this.device = device_1;
    }

    public int method_4()
    {
        return this.int_2;
    }

    public void method_5(int int_3)
    {
        if (int_3 < 20)
        {
            this.int_2 = 20;
        }
        else
        {
            this.int_2 = int_3;
        }
    }

    private void method_8()
    {
        if (this.socket_0 != null)
        {
            this.socket_0.Close();
        }
        this.PLCStatue = 0;
    }

    public bool method_9()
    {
        this.PLCStatue = 0;
        return true;
    }

    public bool PLC_StartConnect()
    {
        bool flag;
        if (flag = this.PLCconnect())
        {
            this.PLCStatue = 2;
            this.StopFlag = false;
            this.thread = new Thread(new ParameterizedThreadStart(this.GetStatue));
            this.thread.IsBackground = true;
            this.thread.Start(this);
            return flag;
        }
        this.PLCStatue = 0;
        return flag;
    }

    public void PLC_StopConnect()
    {
        this.StopFlag = true;
        if ((this.thread != null) && this.thread.IsAlive)
        {
            this.thread.Join(0x3e8);
        }
        this.method_8();
    }

    private bool PLCconnect()
    {
        bool flag2;
        bool flag = false;
        try
        {
            this.PLCStatue = 1;
            if (this.socket_0.Connected)
            {
                this.socket_0.Close();
            }
            this.socket_0 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IAsyncResult asyncResult = this.socket_0.BeginConnect(this.IPSetAdd, this.TCPPort, null, null);
            DateTime now = DateTime.Now;
            while (!asyncResult.IsCompleted)
            {
                TimeSpan span = (TimeSpan) (DateTime.Now - now);
                if (span.TotalMilliseconds > 3000.0)
                {
                    return false;
                }
                Thread.Sleep(50);
            }
            flag = true;
            this.socket_0.EndConnect(asyncResult);
            this.socket_0.ReceiveTimeout = 0xbb8;
            return flag;
        }
        catch (Exception)
        {
            flag2 = false;
        }
        return flag2;
    }

    public void SetIP(string string_1)
    {
        this.IPSetAdd = string_1;
    }

    public void WriteData(string name, double value)
    {
        name = name.Trim().ToUpper();
        char[] separator = new char[] { '.' };
        string[] strArray = name.Split(separator);
        string str = strArray[0];
        string str2 = strArray[1];
        TagGroup group = this.device.TagGroups[str];
        Tag tag = group.Tags[str2];
        e_PLC_MMType type = group.Get_MMtype();
        e_PLC_DATA_TYPE e_plc_data_type = tag.CheckDataType();
        if (this.BackTrue(type, e_plc_data_type))
        {
            new PLCComHelperProj.WriteData(group, tag, value);
            this.device.AddWriteData(group, tag, value);
        }
    }
}

